import {
	apiBaseUrl
} from './config.js';
import * as common from './common.js' //引入common
import * as db from './db.js' //引入common
// 需要登陆的，都写到这里，否则就是不需要登陆的接口
const methodsToken = ['api/wx/decryptphone'];


const post = (method, data, callback, complete) => {
	uni.removeStorage({
		key: 'jump'
	})
	// 获取用户token
	let headers = {
		'Accept': 'application/json',
		'Content-Type': 'application/x-www-form-urlencoded'
	}
	let userToken = db.get("userToken");
	let openid = db.get("openid");
	
	// if (!userToken) {
	// 	common.jumpToLogin();
	// 	return false;
	// } else {
	// 	headers.token=userToken
	// }
	if (userToken) {
		headers.token = userToken
	}
	if (openid) {
		headers.openid = openid
	}
	
	let postData = JSON.parse(JSON.stringify(data))
	delete postData.methodType
	uni.request({
		url: apiBaseUrl + method,
		data: postData,
		header: headers,
		method: data.methodType || 'post',
		success: (response) => {
			const result = response.data
			if (!result.status) {
				// 登录信息过期或者未登录
				if (result.code === 201) {
					db.del("userToken");
					let jump = uni.getStorageSync('jump')
					console.log(jump, 1111)
					if (!jump) {
						uni.setStorageSync('jump', true)
						uni.showToast({
							title: result.message,
							icon: 'none',
							duration: 1500
						});
						setTimeout(() => {

							uni.navigateTo({
								url: '/pages/login/login'
							})
						}, 1500)
					}
				}
			}
			callback(result);
		},

		complete: (response) => {
			setTimeout(function() {
				uni.hideLoading();
			}, 1000)
			complete ? complete() : "";
		},
		fail: (error) => {
			uni.showLoading({
				title: '网络开小差了'
			});
			// setTimeout(function() {
			uni.hideLoading();
			// }, 1000)
			if (error && error.response) {
				showError(error.response);
			} else {

			}

		},
	});

}
const get = (url, callback) => {
	uni.showLoading({
		title: '加载中'
	});
	uni.request({
		url: url,
		header: {
			'Accept': 'application/json',
			'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
		},
		method: 'GET',
		success: (response) => {
			uni.hideLoading();
			callback(response.data);
		},
		fail: (error) => {
			uni.hideLoading();
			if (error && error.response) {
				showError(error.response);
			}
		},
		complete: () => {
			setTimeout(function() {
				uni.hideLoading();
			}, 250);
		}
	});
}

const showError = error => {
	let errorMsg = ''
	switch (error.status) {
		case 400:
			errorMsg = '请求参数错误'
			break
		case 401:
			errorMsg = '未授权，请登录'
			break
		case 403:
			errorMsg = '跨域拒绝访问'
			break
		case 404:
			errorMsg = `请求地址出错: ${error.config.url}`
			break
		case 408:
			errorMsg = '请求超时'
			break
		case 500:
			errorMsg = '服务器内部错误'
			break
		case 501:
			errorMsg = '服务未实现'
			break
		case 502:
			errorMsg = '网关错误'
			break
		case 503:
			errorMsg = '服务不可用'
			break
		case 504:
			errorMsg = '网关超时'
			break
		case 505:
			errorMsg = 'HTTP版本不受支持'
			break
		default:
			errorMsg = error.msg
			break
	}

	uni.showToast({
		title: errorMsg,
		icon: 'none',
		duration: 1000,
		complete: function() {
			setTimeout(function() {
				uni.hideToast();
			}, 1000);
		}
	});
}

// 文件上传
export const uploadFiles = (callback) => {
	uni.chooseImage({
		success: (chooseImageRes) => {
			uni.showLoading({
				title: '上传中...'
			});
			const tempFilePaths = chooseImageRes.tempFilePaths;
			const uploadTask = uni.uploadFile({
				url: apiBaseUrl + 'api/file/upload', //仅为示例，非真实的接口地址
				filePath: tempFilePaths[0],
				fileType: 'image',
				name: 'file',
				header: {

					'Accept': 'application/json',
					'Content-Type': 'application/x-www-form-urlencoded',
					'token': db.get("userToken")
				},
				formData: {
					'file': tempFilePaths[0],

				},
				success: (uploadFileRes) => {
					callback(JSON.parse(uploadFileRes.data));
				},
				fail: (error) => {
					if (error && error.response) {
						showError(error.response);
					}
				},
				complete: () => {
					setTimeout(function() {
						uni.hideLoading();
					}, 250);
				}
			});
		}
	});
}


// 首页轮播图
export const adList = (data, callback) => post('api/wash/banner', data, callback);
export const shopList = (data, callback) => post('api/wash/shopList', data, callback);
export const shopDetail = (data, callback) => post('api/wash/shopDetail', data, callback);
export const wxPhone = (data, callback) => post('api/wanlshop/user/phone', data, callback);

export const goodsList = (data, callback) => post('api/wash/goodsList', data, callback);
export const carList = (data, callback) => post('api/car/index', data, callback);
export const carDel= (data, callback) => post('api/car/del', data, callback);
export const carAdd= (data, callback) => post('api/car/add', data, callback);
export const payment= (data, callback) => post('api/wanlshop/pay/payment', data, callback);
export const orderList= (data, callback) => post('api/wash/order', data, callback);
export const config= (data, callback) => post('api/wash/config', data, callback);

